package com.onetime.mall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.onetime.common.utils.R;
import com.onetime.mall.auth.feign.MemberFeignService;
import com.onetime.mall.auth.vo.UserLoginVo;
import com.onetime.mall.auth.vo.UserRegistrationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Lenovo
 */
@Controller
public class LoginController {
    @Autowired
    MemberFeignService memberFeignService;

    /**
     * @param redirectAttributes 模拟重定向携带数据,通过session获取数据
     */
    @PostMapping("/regist")
    public String registration(@Valid UserRegistrationVo vo, BindingResult result, RedirectAttributes redirectAttributes, Model model) {
                model.addAttribute("msg01", "This is your message");
        model.addAttribute("errors",null);
        System.out.println("result=" + result.getAllErrors());
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            /*不起作用*/
            redirectAttributes.addFlashAttribute("errors", errors);
            System.out.println("注册信息有错误！"+errors);
//            model.addAttribute("errors",errors);
//            return "signup";
            return "redirect:http://auth.mall.com/signup.html";
        }
        /*调用远程服务进行注册*/
        System.out.println("调用远程服务进行注册");
        R r = memberFeignService.regist(vo);
        if (r.getCode() == 0) {
            //成功,转到登录页
            return "redirect:http://auth.mall.com/signin.html";
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("msg", r.get("msg"));
            System.out.println(map);
            redirectAttributes.addFlashAttribute("errors", map);
            return "redirect:http://auth.mall.com/signup.html";
        }

    }


    @PostMapping("/login")
    public String login(UserLoginVo vo, RedirectAttributes redirectAttributes, HttpSession session) {
        //远程登录
        R r = memberFeignService.login(vo);
        System.out.println("======" + r);
        if (r.getCode() == 0) {
/*            MemberRespVo data = r.getData(new TypeReference<MemberRespVo>() {
            });
            session.setAttribute(AuthServerConstant.SESSION_LOGIN_KEY,data);*/
            return "redirect:http://mall.com";
        } else {
            Map<String, String> errors = new HashMap<>();
            errors.put("msg", r.getData(new TypeReference<>() {
            }));
            redirectAttributes.addFlashAttribute("errors", errors);
            return "redirect:http://auth.mall.com/signin.html";
        }
    }

    @RequestMapping("/test")
    public String index(Model model,RedirectAttributes redirectAttributes){
        model.addAttribute("msg","2121212121211");
        String errors=null;
        redirectAttributes.addFlashAttribute("errors",errors);

        return "test";
    }

}
